package atm.zhaz;

import atm.zhaz.client.Client;

public class MyClient {
	Client gClient;
	String username = "";
	String password = "";

	public MyClient(String host, int port){
		gClient = new Client(host, port);
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public boolean auth() {
		String request = String.format(Client.REQUEST_FORMAT, getUsername(), getPassword(), "AUTH", "");
		String response = null;
		try {
			response = gClient.sendRequest(request);
			String[] r = response.split(":");
			if (r[0].equals("OK"))
				return true;
		} catch (Exception e) {

		}
		return false;
	}

	public long deposit(long k) {
		String request = String.format(Client.REQUEST_FORMAT, getUsername(),
				getPassword(), "DEPOSIT", String.format("AMOUNT=%d;", k));
		String response = null;
		try {
			response = gClient.sendRequest(request);
			String[] r = response.split(":");
			if (r[0].equals("OK"))
				return Long.parseLong(r[1]);
		} catch (Exception e) {

		}
		return -1;
	}

	public long balance() {
		String request = String.format(Client.REQUEST_FORMAT, getUsername(),
				getPassword(), "BALANCE", "");
		String response = null;
		try {
			response = gClient.sendRequest(request);
			String[] r = response.split(":");
			if (r[0].equals("OK"))
				return Long.parseLong(r[1]);
		} catch (Exception e) {

		}
		return -1;
	}

	public long withdraw(long k) {
		String request = String.format(Client.REQUEST_FORMAT, getUsername(),
				getPassword(), "WITHDRAW", String.format("AMOUNT=%d;", k));
		String response = null;
		try {
			response = gClient.sendRequest(request);
			String[] r = response.split(":");
			if (r[0].equals("OK"))
				return Long.parseLong(r[1]);
		} catch (Exception e) {

		}
		return -1;
	}

	public long send(long k, String to) {
		String request = String
				.format(Client.REQUEST_FORMAT, getUsername(), getPassword(),
						"SEND", String.format("AMOUNT=%d;TO=%s;", k, to));
		String response = null;
		try {
			response = gClient.sendRequest(request);
			String[] r = response.split(":");
			if (r[0].equals("OK"))
				return Long.parseLong(r[1]);
		} catch (Exception e) {

		}
		return -1;
	}

}
